from app.model.content.wtrees_article import ArticleModel
from app.serve.wtrees.db import db
from app.util.page import Page


class ArticleServe(ArticleModel):

    @classmethod
    def article_create(cls, title, abstract, source, nature, show_time, column_id, content):
        cls.create(title=title, abstract=abstract, source=source, nature=nature, show_time=show_time,
                   column_id=column_id, content=content)
        db.session.commit()

    @classmethod
    def article_after(cls, title, abstract, source, nature, show_time, column_id, content, aid):
        cls.get(id=aid).update(title=title, abstract=abstract, source=source, nature=nature, show_time=show_time,
                               column_id=column_id, content=content)
        db.session.commit()

    @classmethod
    def article_list(cls, search):
        article_list = cls.query
        if search:
            article_list = article_list.filter(cls.title.like("%{}%".format(search)))
        page = Page(article_list)
        tmp = list()
        for article in page.items:
            tmp.append(article.get_info())

        return tmp, page.page_info()

    @classmethod
    def article_detail(cls, id):
        data = cls.get(id=id)

        return data
